package iorder

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured


class DashboardController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def index() {
        redirect(action: "list", params: params)
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [dashboardInstanceList: Dashboard.list(params), dashboardInstanceTotal: Dashboard.count()]
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def create() {
        [dashboardInstance: new Dashboard(params)]
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def save() {
        def dashboardInstance = new Dashboard(params)
        if (!dashboardInstance.save(flush: true)) {
            render(view: "create", model: [dashboardInstance: dashboardInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), dashboardInstance.id])
        redirect(action: "show", id: dashboardInstance.id)
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def show() {
        def dashboardInstance = Dashboard.get(params.id)
        if (!dashboardInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "list")
            return
        }

        [dashboardInstance: dashboardInstance]
    }

    @Secured(['IS_AUTHENTICATED_REMEMBERED'])    
    def edit() {
        def dashboardInstance = Dashboard.get(params.id)
        if (!dashboardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "list")
            return
        }

        [dashboardInstance: dashboardInstance]
    }

    @Secured(['IS_AUTHENTICATED_REMEMBERED'])    
    def update() {
        def dashboardInstance = Dashboard.get(params.id)
        if (!dashboardInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (dashboardInstance.version > version) {
                dashboardInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'dashboard.label', default: 'Dashboard')] as Object[],
                          "Another user has updated this Dashboard while you were editing")
                render(view: "edit", model: [dashboardInstance: dashboardInstance])
                return
            }
        }

        dashboardInstance.properties = params

        if (!dashboardInstance.save(flush: true)) {
            render(view: "edit", model: [dashboardInstance: dashboardInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), dashboardInstance.id])
        redirect(action: "show", id: dashboardInstance.id)
    }
    
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def delete() {
        def dashboardInstance = Dashboard.get(params.id)
        if (!dashboardInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "list")
            return
        }

        try {
            dashboardInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'dashboard.label', default: 'Dashboard'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
